package com.hfzy.ihk.web.oauth.server.controller;


import com.hfzy.ihk.facade.userCenter.entity.Resource;
import com.hfzy.ihk.facade.userCenter.entity.UserHasCallLine;
import com.hfzy.ihk.facade.userCenter.entity.UserInfo;
import com.hfzy.ihk.facade.userCenter.service.IUserHasCallLineService;
import com.hfzy.ihk.facade.userCenter.vo.LoginUserInfo;
import com.hfzy.ihk.web.oauth.server.mapper.UserHasCallLineMapper;
import com.hfzy.ihk.web.oauth.server.mapper.UserMapper;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用于集成spring security 前端控制器
 * </p>
 *
 * @author wws
 * @since 2018-12-28
 */
@RestController
//@RequestMapping("/user")
public class UserController {

    @Autowired(required = false)
    private UserMapper userMapper;

    @Autowired(required = false)
    private UserHasCallLineMapper userHasCallLineMapper;

    /**
     * 资源服务器提供的受保护接口
     * @param principal
     * @return
     */
    @RequestMapping({"/user/me","/me" })
    public LoginUserInfo user(Principal principal) {
        System.out.println("======================>获取用户信息："+principal);
        UserInfo userInfo = userMapper.queryUserInfoByUserName(principal.getName());//findUserByUserName(principal.getName());

        if(userInfo!=null) {
            List<Resource> resources = userMapper.querybtnPermissionByUserName(principal.getName());
            userInfo.setResources(resources);
            List<UserHasCallLine> userHasCallLines = userHasCallLineMapper.getUserHasCallLineByUserId(userInfo.getId());
            userInfo.setUserHasCallLines(userHasCallLines);
        }
        return new LoginUserInfo(userInfo);


    }


    @RequestMapping("apiTest")
    public Map<String,Object> apiTest(){
        Map<String,Object> map = new HashMap<>();
        map.put("result","你好，测试成功");
        return map;

    }

}
